<%--
  Created by IntelliJ IDEA.
  User: hejx
  Date: 2017/5/19
  Time: 15:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
    <title>${sessionScope.appName}-找回密码</title>
    <jsp:include page="/template/header.jsp" />
    <script>
        if (window != top){
            top.location.href = location.href;
        }
    </script>
</head>
<body class="gray-bg">

    <div ng-app="myApp" ng-controller="findPwdController" class="middle-box text-center loginscreen  animated fadeInDown">
        <div>
            <h3>找回密码</h3>
            <form class="m-t" role="form" method="post">
                <div class="form-group">
                    <input type="text" class="form-control" ng-model="param.phone" maxlength="11" name="phone" id="phone" placeholder="手机号" required="">
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" ng-model="param.newPassword" name="newPassword" id="newPassword" placeholder="新密码" required="">
                </div>
                <div class="form-group">
                    <input type="password" class="form-control" ng-model="param.newPassword2" name="newPassword2" id="newPassword2" placeholder="确认新密码" required="">
                </div>
                <div class="form-group">
                    <div style="width: 50%;float: left;">
                        <input type="text" class="form-control" ng-model="param.identifyCode" name="identifyCode" id="identifyCode" placeholder="短信验证码" required="">
                    </div>
                    <div style="width: 45%;float: right;">
                        <button type="button" ng-click="getIdentifyCode($event)" id="getIdentifyCode" class="btn m-b-xs w-xs btn-primary">获取短信验证码</button>
                    </div>
                    <div class="clear"></div>
                </div>
                <button id="subBtn" ng-click="save()" type="button" class="btn btn-primary block full-width m-b" href="JavaScript:void(0);">提 交</button>
            </form>
        </div>
    </div>

    <!-- 导入尾部公共js -->
    <jsp:include page="/template/tail.jsp" />
    <!-- Sweet Alert -->
    <link href="<%=basePath%>static/css/plugins/sweetalert/sweetalert.css" rel="stylesheet">
    <script src="<%=basePath%>static/js/plugins/sweetalert/sweetalert.min.js"></script>
</body>
<script>

    app.controller('findPwdController', function($scope,$http) {

        $scope.URL = {
            GET_IDENTIFYCODE_URL:"/getIdentifyCode",
            FIND_PWD_URL:"/findPwd"
        };

        $scope.param = {
            phone:"",
            newPassword:"",
            newPassword2:"",
            identifyCode:""
        };

        $scope.countdown = 60;
        $scope.setTime = function(elem){
            if ($scope.countdown == 0) {
                elem.removeAttribute("disabled");
                elem.innerHTML = "获取短信验证码";
                $scope.countdown = 60;
            } else {
                elem.setAttribute("disabled", true);
                elem.innerHTML = "重新发送(" + $scope.countdown + ")";
                $scope.countdown--;
                setTimeout(function() {
                    $scope.setTime(elem);
                },1000)
            }
        };

        //校验参数 return boolean
        $scope.validateParam = function(){
            if(!validatemobile($scope.param.phone)){
                layer.msg(common.ERR_MSG.PHONE_ERROR_MSG);
                return true;
            }
            if(isNull($scope.param.newPassword)){
                layer.msg("请输入新密码");
                return true;
            }
            if($scope.param.newPassword!=$scope.param.newPassword2){
                layer.msg("两次输入的新密码不相同");
                return true;
            }
            if(isNull($scope.param.identifyCode)){
                layer.msg("请输入短信验证码");
                return true;
            }
            return false;
        }

        //save操作
        $scope.save = function(){

            if($scope.validateParam()){
                return false;
            }

            $('#subBtn')[0].disabled = true;
            //提交数据
            $http({method : 'POST', data:$scope.param, url : $scope.URL.FIND_PWD_URL})
                .success(function(data,status,headers,config){
                    $('#subBtn')[0].disabled = false;
//                    layer.msg(data.msg);
                    if(data.code==0){
                        alert("新密码设置成功，点击确定跳转到登录页面！");
                        window.parent.location.href = "/login";
                    }
                }).error(function(data,status,headers,config){
                $('#subBtn')[0].disabled = false;
                console.log('error..........');
                console.log(data);
            });
        }

        //获取短信验证码
        $scope.getIdentifyCode = function($event){
            var elem = $event.target;
            if(!validatemobile($scope.param.phone)){
                layer.msg(common.ERR_MSG.PHONE_ERROR_MSG);
                return false;
            }
            //发送短信
            $http.get($scope.URL.GET_IDENTIFYCODE_URL,{params:{phone:$scope.param.phone,type:1}}).success(function(data,status,headers,config){
                if(data.code==0){
                    layer.msg("短信已发送");
                    $scope.setTime(elem);
                }
            }).error(function(data,status,headers,config){
                console.log('error..........');
                console.log(data);
            })

        };


    });


</script>

</html>
